package com.dsp.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.core.util.PinYin;
import com.dsp.bean.Message;
import com.dsp.bean.National;
import com.dsp.services.NationalService;

@Controller("NationalsController")
public class NationalsController extends DspController<National>{

	public NationalsController() {
		super(National.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@Autowired
	private NationalService nationalService;

	@RequestMapping(value = "/nationals", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/national/list");
		mav.addObject("current", "national");
		return mav;
	}

	@RequestMapping(value = "/nationals_list")
	@ResponseBody
	public Pagination<National> index(HttpServletRequest request, HttpServletResponse response) {
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(National.class);
		Pagination<National> p = this.nationalService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value = "/nationals_create", method = RequestMethod.POST)
	@ResponseBody
	public Message create(@RequestBody National national) {
		Message message = new Message();
		logger.info("正在创建民族.....");
		national.setPyCode(new PinYin().toPinYin(national.getnName(), true, false));
		try {
			if (national.getId() != null) {
				this.nationalService.update(national);
			} else {
				this.nationalService.save(national);
			}
		} catch (Exception se) {
			se.getStackTrace();
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(se.getLocalizedMessage());
			message.setMsg(msg);
		}
		logger.info("完成创建民族.....");
		return message;
	}

	@RequestMapping(value = "/nationals_edit", method = RequestMethod.POST)
	@ResponseBody
	public National edit(@RequestBody National _national) {
		National national = this.nationalService.findById(_national.getId());
		return national;
	}

	@RequestMapping(value = "/nationals_delete", method = RequestMethod.POST)
	@ResponseBody
	public Message delete(@RequestBody National national) {
		Message message = new Message();
		String succ = this.nationalService.delete(national);
		message.setSuccess(Boolean.valueOf(succ));
		return message;
	}

	/**
	 * @return the nationalService
	 */
	public NationalService getNationalService() {
		return nationalService;
	}

	/**
	 * @param nationalService
	 *            the nationalService to set
	 */
	public void setNationalService(NationalService nationalService) {
		this.nationalService = nationalService;
	}

}
